//上移
var move_up = function(){
	$.each($("tbody").children("tr"),function(index,value){
		if("rgb(152, 191, 33)" == $(this).css("background-color")){
			var prev_element = $(this).prev();
			prev_element.before($(this));//在每个匹配的元素之前插入内容。
		}
	});
}

//添加
var add = function(){
	if($.trim($("input[name='total_length']").val()) == ""){
		var obj = $("input[name='total_length']");
		obj.focus();
		layer.tips('请先填写库线总长度!', obj);
		return false;
	}
	layer.open({
	  	title:'添加库线长度'
		,content: '<div class="form-window"><div class="input-text">名称：<input type="text" name="line_name"></div>'+
		          '<div class="input-text">长度：<input type="text" name="line_length"></div></div>'
    	,area: ['300px', '300px']
	    ,scrollbar: false
	  	,btn: ['确认', '取消']
		,yes: function(index,layero){ //确认
			if(add_base_line()){
				layer.close(index);
			};
		}
});
}

//保存库线长度
var add_base_line = function(){
	var name = $("input[name='line_name']").val();
	var length = $("input[name='line_length']").val();
	if(name == ""){
		var obj = $("input[name='line_name']");
		obj.focus();
		layer.tips('请填写库线名!', obj);
		return false;
	}
	if(length == ""){
		var obj = $("input[name='line_length']");
		obj.focus();
		layer.tips('请填写库线长度!', obj);
		return false;
	}
	//判断库线总长
	var total_length = "0";
	$.each($("tbody").children("tr"),function(index,value){
		var line_length = $.trim($(this).find("td").last().text());
		total_length = parseInt(total_length) + parseInt(line_length);
	});
	total_length = parseInt(total_length) + parseInt(length);
	var total = $.trim($("input[name='total_length']").val());
	if(total_length > total){
		layer.msg("库线总长"+total_length+"超过范围值"+total+",请重新设置!", {icon: 2,time:1000});
		return false;
	}
	var add_html = '<tr><td>'+name+'</td><td>'+length+'</td></tr>';
	$("tbody").append(add_html);
	refresh();
	return true;
}

//submitData
var submit_data = function(){
	//提交数据
	var data = {};
	var layout = {};//边跨位置_尺寸链
	var power = {};//地面电源
	var orgin = {};
	var d1 = [];
	var d2 = [];
	var d3 = [];
	var d4 = [];
	var d5 = [];
	var d6 = [];
	var biankua_layout = [];//消防电源
	var biankua_location,xianku4_location,direction,orgin,l1,l2,l3,
	l4,l5,l6,l7,l8,lk,lb;
	biankua_location = $.trim($("input[name='sidespan']").filter(":checked").val());
	biankua_location = biankua_location==undefined?"":biankua_location;
	if(biankua_location == ""){
		var obj = $("input[name='sidespan']");
		obj.focus();
		layer.tips('请选择边跨位置!', obj);
		return false;
	}
	orgin.x = 100000;
	orgin.y = 1000;
	xianku4_location = $.trim($("input[name='4line']").filter(":checked").val());
	xianku4_location = xianku4_location==undefined?"":xianku4_location;
	if(xianku4_location == ""){
		var obj = $("input[name='4line']");
		obj.focus();
		layer.tips('请选择四线库位置!', obj);
		return false;
	}
	direction = $.trim($("input[name='storage']").filter(":checked").val());
	direction = direction==undefined?"":direction;
	if(direction == ""){
		var obj = $("input[name='storage']");
		obj.focus();
		layer.tips('请选择入库方向!', obj);
		return false;
	}
	l1 = $.trim($("input[name='l1']").val());
	l1 = l1==undefined?"":l1;
	if(l1 == ""){
		var obj = $("input[name='l1']");
		obj.focus();
		layer.tips('请选择尺寸链L1', obj);
		return false;
	}
	
	l2 = $.trim($("input[name='l2']").val());
	l2 = l2==undefined?"":l2;
	if(l2 == ""){
		var obj = $("input[name='l2']");
		obj.focus();
		layer.tips('请选择尺寸链L2', obj);
		return false;
	}
	
	l3 = $.trim($("input[name='l3']").val());
	l3 = l3==undefined?"":l3;
	if(l3 == ""){
		var obj = $("input[name='l3']");
		obj.focus();
		layer.tips('请选择尺寸链L3', obj);
		return false;
	}
	
	l4 = $.trim($("input[name='l4']").val());
	l4 = l4==undefined?"":l4;
	if(l4 == ""){
		var obj = $("input[name='l4']");
		obj.focus();
		layer.tips('请选择尺寸链L4', obj);
		return false;
	}
	
	l5 = $.trim($("input[name='l5']").val());
	l5 = l5==undefined?"":l5;
	if(l5 == ""){
		var obj = $("input[name='l5']");
		obj.focus();
		layer.tips('请选择尺寸链L5', obj);
		return false;
	}
	
	l6 = $.trim($("input[name='l6']").val());
	l6 = l6==undefined?"":l6;
	if(l6 == ""){
		var obj = $("input[name='l6']");
		obj.focus();
		layer.tips('请选择尺寸链L6', obj);
		return false;
	}
	
	l7 = $.trim($("input[name='l7']").val());
	l7 = l7==undefined?"":l7;
	if(l7 == ""){
		var obj = $("input[name='l7']");
		obj.focus();
		layer.tips('请选择尺寸链L7', obj);
		return false;
	}
	
	l8 = $.trim($("input[name='l8']").val());
	l8 = l8==undefined?"":l8;
	if(l8 == ""){
		var obj = $("input[name='l8']");
		obj.focus();
		layer.tips('请选择尺寸链L8', obj);
		return false;
	}
	
	lk = $.trim($("input[name='lk']").val());
	lk = lk==undefined?"":lk;
	if(lk == ""){
		var obj = $("input[name='lk']");
		obj.focus();
		layer.tips('请选择尺寸链Lk', obj);
		return false;
	}
	
	lb = $.trim($("input[name='lb']").val());
	lb = lb==undefined?"":lb;
	if(lb == ""){
		var obj = $("input[name='lb']");
		obj.focus();
		layer.tips('请选择尺寸链Lb', obj);
		return false;
	}
	//地面电源
	$.each($("input[name='D1']").filter(":checked"),function(){
		d1.push($(this).val());
	});
	$.each($("input[name='D2']").filter(":checked"),function(){
		d2.push($(this).val());
	});
	$.each($("input[name='D3']").filter(":checked"),function(){
		d3.push($(this).val());
	});
	$.each($("input[name='D4']").filter(":checked"),function(){
		d4.push($(this).val());
	});
	$.each($("input[name='D5']").filter(":checked"),function(){
		d5.push($(this).val());
	});
	$.each($("input[name='D6']").filter(":checked"),function(){
		d6.push($(this).val());
	});
	$.each($("#kxcd tbody tr"),function(){
		var name = $(this).children("td").first().text();
		var width = $(this).children("td").last().text();
		var obj_json = {};
		obj_json.name = name;
		obj_json.width = parseInt(width);
		biankua_layout.push(obj_json);
	});
	layout["biankua_location"] = biankua_location;
	layout["xianku4_location"] = xianku4_location;
	layout["direction"] = direction;
	layout["l1"] = parseInt(l1);
	layout["l2"] = parseInt(l2);
	layout["l3"] = parseInt(l3);
	layout["l4"] = parseInt(l4);
	layout["l5"] = parseInt(l5);
	layout["l6"] = parseInt(l6);
	layout["l7"] = parseInt(l7);
	layout["l8"] = parseInt(l8);
	layout["lk"] = parseInt(lk);
	layout["lb"] = parseInt(lb);
	layout["orgin"] = orgin;
	power["d1"] = d1;
	power["d2"] = d2;
	power["d3"] = d3;
	power["d4"] = d4;
	power["d5"] = d5;
	power["d6"] = d6;
	data.layout = layout;
	data.power = power;
	data.biankua_layout = biankua_layout;
	data.project_type = "BatchJianXiuKu6";
	var data_json_str = JSON.stringify(data);
	//提交更新
	var url = base+"/projectCenter/singleDrawDesign/getJsonSingleDrawDesign/"+data_json_str;
	window.open(url,"_self");
	return false;
}

//下移
var move_down = function(){
	$.each($("tbody").children("tr"),function(index,value){
		if("rgb(152, 191, 33)" == $(this).css("background-color")){
            var next_element = $(this).next();
            next_element.after($(this));//在每个匹配的元素之后插入内容。
		}
	});
}

//删除
var del = function(){
	   layer.confirm('确认删除?',{btn : [ '确定', '取消' ]},function(index){
		   layer.close(index);
			$.each($("tbody").children("tr"),function(index,value){
				if("rgb(152, 191, 33)" == $(this).css("background-color")){
					$(this).remove();
					refresh();
				}
			});
	   });
}

$("body").delegate("#kxcd tbody tr","click",function(){
	$(this).siblings().css("background-color","");
	$(this).css("background-color","#98bf21");
})

var refresh = function(){
	var total_length = "0";
	$.each($("tbody").children("tr"),function(index,value){
		var line_length = $.trim($(this).find("td").last().text());
		total_length = parseInt(total_length) + parseInt(line_length);
	});
	$("input[name='instantly-length']").val(total_length);
}
$("#move_up").click(move_up);
$("#move_down").click(move_down);
$("#add").click(add);
$("#del").click(del);
$(function(){
	refresh();
}); 